package BTconn;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;



/**
 * GUI of the application
 */
public class MIDmenu
{

    /**
     * A menu list instance
     */
    public final List menu = new List (MAIN_MENU_TITLE, List.IMPLICIT, elements, null);

    /**
     * A list of menu items
     */
    private static final String[] elements = {"Find Devices", "Exit"};

    /**
     * A devices list instance
     */
    public List devicesList;

    /**
     * A services list instance
     */
    public List servicesList;

    /**
     * A list of devices' friendly names
     */
    private String[] devElements;

    private String[] servicesElements;

    /**
     * An instance of a Controler class
     */
    private Controler controler;
    
    /**
     * Exit button
     */
    public final Command EXIT_CMD = new Command("Exit", Command.EXIT, 2);

    /**
     * Approval button
     */
    public final Command OK_CMD = new Command("Ok", Command.SCREEN, 1);

    /**
     * Back to previous menu button
     */
    public final Command BACK_CMD = new Command("Back", Command.BACK, 2);

    /**
     * Refresh devices list button
     */
    public final Command REFRESH_CMD = new Command("Refresh", Command.OK, 1);


    /**
     * value is true after creating the server/client
     */
    private boolean isInit = false;

    /**
     * Constant value representing a number which determines that a List-of-devices
     * type of a list is going to be displayed
     */
    public static final int DEV_MENU = 1;

    /**
     * Constant value representing a number which determines that a Main-menu
     * type of a list is going to be displayed
     */
    public static final int MAIN_MENU = 0;

    /**
     * Constant value representing a number which determines that a List-of-services
     * type of a list is going to be displayed
     */
    public static final int SERVICES_MENU = 2;

    /**
     * Constant value representing a title of a devices menu list
     */
    public static final String DEV_MENU_TITLE = "GPS-BT Receiver - Found Devices";

    /**
     * Constant value representing a title of a main menu list
     */
    public static final String MAIN_MENU_TITLE = "GPS-BT Receiver - Main Menu";

    /**
     * Constant value representing a title of a main menu list
     */
    public static final String SERVICES_MENU_TITLE = "GPS-BT Receiver - Found Services";
    /**
     * Constant value informing that the devices are being searched for
     */
    public static final String WAIT_DEV_TITLE = "Looking for devices...";

    public static final String WAIT_SER_TITLE = "Looking for services...";

    public static final String NO_DEVICES = "No devices were found!";

    public static final String NO_SERVICES = "No services were found!";

    public boolean isMenuOpened = false;
    
    public boolean isDevMenuOpened = false;

    public boolean isServMenuOpened = false;

    
    /**
     * Constructs main screen of the MIDlet
     */
    public MIDmenu (Controler c)
    {
        controler = c;
        menu.addCommand (EXIT_CMD);
        menu.addCommand (OK_CMD);
        menu.setCommandListener(controler);


    }

    /**
     * Gets the index of a selected list item
     */
    public int getSelectedListIndex(List list)
    {
        return list.getSelectedIndex();
    }
    

    /**
     * Returns the displayable object of this screen
     */
    Displayable getDisplayable()
    {
        return menu;
    }

    /**
     * Sets the current (active) list to be displayed by the num parameter
     */
    public void setActiveMenu(int num)
    {
        switch(num)
        {
            case MAIN_MENU:
                Display.getDisplay(controler).setCurrent(menu);
                isMenuOpened = true;
                isDevMenuOpened = false;
                isServMenuOpened = false;

                break;

            case DEV_MENU:
                devicesList = new List(DEV_MENU_TITLE, List.IMPLICIT, devElements, null);
                devicesList.addCommand(BACK_CMD);
                devicesList.addCommand(REFRESH_CMD);
                devicesList.setCommandListener(controler);
                Display.getDisplay(controler).setCurrent(devicesList);
                isDevMenuOpened = true;
                isMenuOpened = false;
                isServMenuOpened = false;

                break;

            case SERVICES_MENU:
                servicesList = new List(SERVICES_MENU_TITLE, List.IMPLICIT, servicesElements, null);
                servicesList.addCommand(BACK_CMD);
                servicesList.addCommand(REFRESH_CMD);
                servicesList.setCommandListener(controler);
                Display.getDisplay(controler).setCurrent(servicesList);
                isServMenuOpened = true;
                isMenuOpened = false;
                isDevMenuOpened = false;

                break;


            default:
                System.err.println("Wrong menu number!!");
                break;
        }
        
            
    }

    /**
     * Sets the String table value with a devices' friendly names
     */
    public void setDevElements(String[] dE)
    {
        devElements = dE;
    }

    public void setServicesElements(String[] sE)
    {
        servicesElements = sE;
    }

    public void refreshList(List list)
    {
        for (int i=0; i<list.size(); i++)
                list.delete(i);
        if (list==devicesList)
        {
            for (int i=0; i<devElements.length; i++)
                list.append(devElements[i], null);
        }
        else if (list==servicesList)
        {
            for (int i=0; i<servicesElements.length; i++)
                list.append(servicesElements[i], null);
        }

    }

//    public void refreshSerList()
//    {
//        for (int i=0; i<servicesList.size(); i++)
//            servicesList.delete(i);
//        for (int i=0; i<servicesList.length; i++)
//            servicesList.append(servicesList[i], null);
//    }

    /**
     * Adds given by a parameter label to chosen by a number list
     */
    public void addLabelOnMenuPosition(String label, int nr)
    {
        switch(nr)
        {
            case MAIN_MENU:
                menu.append(label, null);
                break;

            case DEV_MENU:
                if (devicesList.size() > 0 && devicesList.getString(0).equals(WAIT_DEV_TITLE))
                    devicesList.delete(0);
                
                devicesList.append(label, null);
                break;

            case SERVICES_MENU:
                if (servicesList.size() > 0 && servicesList.getString(0).equals(WAIT_SER_TITLE))
                    servicesList.delete(0);

                servicesList.append(label, null);
                break;


            default:
                System.err.println("Trying to add label to a non-existing menu!");
                break;
        }
    }

    /**
     * Clears the chosen by a parameter menu
     */
    public void clearMenu(int nr)
    {
        switch(nr)
        {
            case MAIN_MENU:
                for (int i=menu.size()-1; i>-1; i--)
                    menu.delete(i);
                
                break;

            case DEV_MENU:
                for (int i=devicesList.size()-1; i>-1; i--)
                    devicesList.delete(i);

                break;

            case SERVICES_MENU:
                for (int i=servicesList.size()-1; i>-1; i--)
                    servicesList.delete(i);

                break;
        }
    }
}
